import {_MANGA_PATH_} from '../../js/my-core'
import {showMessage} from '../../js/my-plugin'
import {publicAcgInit} from '../../js/my-public'

export const getByAv = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/manga/play/eq/' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        publicAcgInit(_this, _this.manga, res.data.data)
        let author = ''
        for (let item of res.data.data.author) {
          author += item + '，'
        }
        if (author == null) {
          author = 'NULL'
        } else {
          author = author.substring(0, author.length - 1)
        }
        _this.manga.author = author
        _this.mangaDetails = []
        let i = 0
        for (let item of res.data.data.mangaDetails) {
          let it = {id: i, title: item.title, auth: item.auth, href: _MANGA_PATH_ + item.id, play: false}
          if (Number(val) === item.id) {
            it.play = true
            _this.currentChapter = i
            _this.txt.title = item.title
            getSource(_this, item.id)
          }
          _this.mangaDetails.push(it)
          i++
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}

export const getByAn = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/manga/play/ss/' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        publicAcgInit(_this, _this.manga, res.data.data)
        let author = ''
        for (let item of res.data.data.author) {
          author += item + '，'
        }
        if (author === '') {
          author = 'NULL'
        } else {
          author = author.substring(0, author.length - 1)
        }
        _this.manga.author = author
        _this.mangaDetails = []
        let i = 0
        for (let item of res.data.data.mangaDetails) {
          let it = {id: i, title: item.title, auth: item.auth, href: _MANGA_PATH_ + item.id, play: false}
          if (i === 0) {
            it.play = true
            _this.currentChapter = 0
            _this.txt.title = item.title
            getSource(_this, item.id)
          }
          _this.mangaDetails.push(it)
          i++
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}

export const getSource = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/manga/play/source/' + val
  })
    .then((res) => {
      _this.txt.content = []
      if (res.data.code === 200) {
        for (let item of res.data.data) {
          const arr = {id: item.id, content: item.item}
          _this.txt.content.push(arr)
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}
